import React from 'react';

class ZoomCtrl extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            infoWindow: {},
            map: {},
            list:{}
        }
        this.count=0
    }
    componentWillMount() {
        const map = this.props.__map__;
        if (!map) {
            console.log('组件必须作为 Map 的子组件使用');
            return;
        }
        this.setState({
                map: map
            }
        )

    }
    componentWillReceiveProps(nextProps){
        console.log(`nextProps.params`,nextProps.params);
        if(nextProps.params&&nextProps.params.length>0){
            console.log(`nextProps.params`,nextProps.params);
            let lnglats=nextProps.params.map((item)=>{
                return [item.gisGcj02Lng,item.gisGcj02Lat]
            })
            let infoWindow = new window.AMap.InfoWindow({offset: new window.AMap.Pixel(0, -30)});
            let  markerClick=(e)=> {
                console.log(e)
                infoWindow.setContent(e.target.content);
                infoWindow.open(this.state.map, e.target.getPosition());
            }
            for (let i = 0; i < lnglats.length; i++) {
                let marker = new window.AMap.Marker({
                    position: lnglats[i],
                    map: this.state.map,
                    autoMove:true
                });
                marker.content = nextProps.params[i].name;
                marker.on('click', markerClick);
                marker.emit('click', {target: marker});
            }
            if(this.count===0){
                this.state.map.setFitView();
            }else{
                this.state.map.setCenter(lnglats[lnglats.length-1])
            }
            this.count++;
        }
    }
    render() {
        return (
            null
        )
    }
}

export default ZoomCtrl